package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.*;
import com.yyzw.activiti.modeler.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(value = "flow")
public class FlowController {

    @Autowired
    FlowService flowService;

    @RequestMapping(value = "getFlowInfo")
    public List<Object> getFlowInfo(HttpServletRequest request)throws  Exception{
        String processInstanceId = request.getParameter("processInstanceId");
        flowService.getHistoryTaskByProcId(processInstanceId);
        return null;
    }

    @RequestMapping(value = "getFlowList")
    public DataGridRespVo getFlowList(HttpServletRequest request, String page, String rows){
        SysUser userSession =(SysUser) request.getSession().getAttribute("userSession");
        int userId = userSession.getUserId();
       return flowService.getFlowList(userId, Integer.parseInt(page),Integer.parseInt(rows));
    }

    @RequestMapping(value = "addFlow")
    public ResponseVo addFlow(@RequestBody SysProjectApply sysProjectApply,HttpServletRequest request){
        SysUser userSession =(SysUser) request.getSession().getAttribute("userSession");
        sysProjectApply.setUserName(userSession.getUserName());
        sysProjectApply.setUserId(userSession.getUserId());
        flowService.addFlow(sysProjectApply);
        //启动流程实例
        return flowService.startFlowAndUpdate(sysProjectApply);
    }

    @RequestMapping(value = "showFlow")
    public ModelAndView showFlowList(HttpServletRequest request,ModelAndView modelAndView) throws Exception{
        modelAndView.setViewName("busflow/busFlowList");
        return modelAndView;
    }

    @RequestMapping(value = "showFlowProcess")
    public List<TaskHandleVo> showFlowProcess(HttpServletRequest request){
        String processInstanceId = request.getParameter("processInstanceId");
        return flowService.getFlowProcess(processInstanceId);
    }

}
